package com.example.androidhive;
import android.app.Activity;
import android.location.Geocoder;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

//import com.example.androidhive.Logowanie.CreateNewProduct1;
//import com.techblogon.serviceexample.MyService;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MojeDane extends Activity {
	private CheckBox chkIos, chkAndroid, chkWindows;
	  private Button btnDisplay;
	  
		private ProgressDialog pDialog;
		MediaPlayer player;
		JSONParser jsonParser = new JSONParser();
		EditText inputName;
		EditText inputPrice;
		EditText inputDesc;
		

		// url to create new product
		private static String url_create_product ="http://opiekuj1.javaprovider.net/php/d_u_wyposarzenie.php"; 

		// JSON Node names
		private static final String TAG_SUCCESS = "success";
		
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my);
		addListenerOnChkIos();
		addListenerOnButton(); //setVolumeControlStream(AudioManager.STREAM_RING);
        //we now instantiate the player using our music file;
        //note that since it is a resource in the "raw" directory,
        //we don't need the file's extension;
        //our file here is in fact called "soundfile.mp3";
		
	
      //  player = MediaPlayer.create(this, R.raw.eee);
        //we start playing the file!
      //  player.start();
	}
	 public void addListenerOnChkIos() {
		
			chkIos = (CheckBox) findViewById(R.id.chkIos);
		 
			chkIos.setOnClickListener(new OnClickListener() {
		 
			  @Override
			  public void onClick(View v) {
					
		                //is chkIos checked?
				/*if (((CheckBox) v).isChecked()) {
					Toast.makeText(MojeDane.this,
				 	   "Bro, try Android :)", Toast.LENGTH_LONG).show();
				}*/
		 
			  }
			});
		 
		  }
		 
		  public void addListenerOnButton() {
		 
			chkIos = (CheckBox) findViewById(R.id.chkIos);
			chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
			chkWindows = (CheckBox) findViewById(R.id.chkWindows);
			btnDisplay = (Button) findViewById(R.id.btnDisplay);
		 
			btnDisplay.setOnClickListener(new OnClickListener() {
		 
		          //Run when button is clicked
			  @Override
			  public void onClick(View v) {
		 
				StringBuffer result = new StringBuffer();
				result.append("IPhone check : ").append(chkIos.isChecked());
				result.append("\nAndroid check : ").append(chkAndroid.isChecked());
				result.append("\nWindows Mobile check :").append(chkWindows.isChecked());
		 
				Toast.makeText(MojeDane.this, result.toString(),
						Toast.LENGTH_LONG).show();
				rr();
				putIntoArrayAndSave();
			  }
			});
		 
		  }
		  void putIntoArrayAndSave(){
			  //aa
				String[] c={"nazwa", "mm","prysznic","1","tv", "1","wifi", "1","suszarka","1",
						"pralka", "1"};
				
				Base_ aa=new Base_(MojeDane.this,c,getApplicationContext());
				aa.execute();			  
		  }
		  void rr(){
			  String youraddress="Kluczbork Jana Pawla II";
			  String uri = "http://maps.google.com/maps/api/geocode/json?address=Dąbrowa,Cherubiny 22&sensor=false";
	    HttpGet httpGet = new HttpGet(uri);
	    HttpClient client = new DefaultHttpClient();
	    HttpResponse response;
	    StringBuilder stringBuilder = new StringBuilder();

	    try {
	        response = client.execute(httpGet);
	        HttpEntity entity = response.getEntity();
	        InputStream stream = entity.getContent();
	        int b;
	        while ((b = stream.read()) != -1) {
	            stringBuilder.append((char) b);
	        }
	    } catch (ClientProtocolException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }

	    JSONObject jsonObject = new JSONObject();
	    try {
	        jsonObject = new JSONObject(stringBuilder.toString());
	        double lat= 0.0, lng= 0.0;
	        lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
	            .getJSONObject("geometry").getJSONObject("location")
	            .getDouble("lng");

	        lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
	            .getJSONObject("geometry").getJSONObject("location")
	            .getDouble("lat");
	    	Toast.makeText(MojeDane.this, lat+"-"+lng,
					Toast.LENGTH_LONG).show();
	        //Log.d("latitude", lat);
	        //Log.d("longitude", lng);
	    } catch (JSONException e) {
	        e.printStackTrace();
	    }
		      
	}
		  

			/**
			 * Background Async Task to Create new product
			 * */
			class CreateNewProduct3 extends AsyncTask<String, String, String> {

				/**
				 * Before starting background thread Show Progress Dialog
				 * */
				@Override
				protected void onPreExecute() {
					super.onPreExecute();
					pDialog = new ProgressDialog(MojeDane.this);
					pDialog.setMessage("Dodawanie update wyposarzenia..");
					pDialog.setIndeterminate(false);
					pDialog.setCancelable(true);
					pDialog.show();
				}

				/**
				 * Creating product
				 * */
				protected String doInBackground(String... args) {

					// Building Parameters
					List<NameValuePair> params = new ArrayList<NameValuePair>();					
					params.add(new BasicNameValuePair("nazwa", "mm"));
					params.add(new BasicNameValuePair("prysznic","1"));
					params.add(new BasicNameValuePair("tv", "1"));
					params.add(new BasicNameValuePair("wifi", "1"));
					params.add(new BasicNameValuePair("suszarka","1"));
					params.add(new BasicNameValuePair("pralka", "1"));
					JSONObject json = jsonParser.makeHttpRequest(url_create_product,
							"POST", params);
					Log.d("Create Response", json.toString());

					// check for success tag
					try {
						int success = json.getInt(TAG_SUCCESS);

						if (success == 1) {
							// successfully created product
							Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
							startActivity(i);
							
							// closing this screen
							finish();
							//inputDesc =inputPrice =inputName = null;
						} else {
							Intent i = new Intent(getApplicationContext(), Logowanie.class);
							startActivity(i);
							// failed to create product
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}

					return null;
				}

				/**
				 * After completing background task Dismiss the progress dialog
				 * **/
				protected void onPostExecute(String file_url) {
					// dismiss the dialog once done
					pDialog.dismiss();
				}

			}

}